package com.saunter.filter;

import com.saunter.utils.CookieUtils;
import com.saunter.utils.encrypt.JavaWebToken;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(urlPatterns = {"/admin/*"},filterName = "adminFilter")
public class AdminFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request= (HttpServletRequest) servletRequest;

        if(!"/admin/login".equals(request.getRequestURI())) {
            Object admin_id = JavaWebToken.parseJavaWebToken(CookieUtils.getCookie(request, "sid"), "admin_id");
            CusRequestWrapper cusRequestWrapper = new CusRequestWrapper(request);
            cusRequestWrapper.addParameterMap("system_append_admin_id", null == admin_id ? null : admin_id.toString());
            filterChain.doFilter(cusRequestWrapper,servletResponse);
        }else {
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
